package com.awspaas.user.apps.shhtaerospaceindustrial.healthmeaasge;

import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.commons.formfile.model.delegate.FormFile;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.bind.annotation.Controller;
import com.actionsoft.bpms.server.bind.annotation.Mapping;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

import java.util.List;
import java.util.Map;

@Controller
public class DrugWarehouseQueryCmd {
	/**
	 * @Desc 根据药品库信息查询药品库地点信息
	 * @param uc
	 * @param
	 * @return
	 */
	@Mapping("com.awspaas.user.apps.DrugAddress")
	public static String getDrugAddress(UserContext uc) {
		JSONObject result = new JSONObject();
		try {
			String queryUrl ="SELECT distinct(GRIDPOINTNAME),WGDID FROM VIEW_SHHT_DRUGLIST  " ;
			List<Map<String, Object>> urlList = DBSql.query(queryUrl, new ColumnMapRowMapper(), new Object[]{});
			if(urlList == null || urlList.isEmpty()) {
				result.put("status", "1");
				result.put("message", "未查询到该服务点信息。");
				return result.toString();
			}
			JSONArray tececaiArr = new JSONArray();
			for (int i = 0; i < urlList.size(); i++) {
				Map<String, Object> urlMap = urlList.get(i);
				JSONObject tececaitem = new JSONObject();
				String WGDID = CoreUtil.objToStr(urlMap.get("WGDID"));//网格点id
				String WGDNAME = CoreUtil.objToStr(urlMap.get("GRIDPOINTNAME"));//网格点名称
				tececaitem.put("wgdid", WGDID);
				tececaitem.put("wgdname", WGDNAME);
				tececaiArr.add(tececaitem);
			}
				result.put("status", "0");
				result.put("tececaiArr", tececaiArr);

		} catch (Exception e) {
			e.printStackTrace();
			result.put("status", "1");
			result.put("message", e.getMessage());
		}
		return result.toString();
	}
	
	/**
	 * @Desc 根据网格点地址，查询该网格点药品信息
	 * @param uc
	 * @param wgdname  药品地址
	 * @return
	 */
	@Mapping("com.awspaas.user.apps.DrugInformation")
	public static String getDrugInformation(UserContext uc, String wgdname) {
		JSONObject result = new JSONObject();
		try {
			String queryUrl =
			    "SELECT BINDID,ID,TRADENAME,DRUGNAME,DRUGSPEC,DRUGUNIT,DRUGPRICE,MANUFACTURER FROM VIEW_SHHT_DRUGLIST " +
					" WHERE GRIDPOINTNAME =  '"+wgdname+"'";
			List<Map<String, Object>> urlList = DBSql.query(queryUrl, new ColumnMapRowMapper(), new Object[]{});
			if(urlList == null || urlList.isEmpty()) {
				result.put("status", "1");
				result.put("message", "未查询到该服务点提供的药品信息。");
				return result.toString();
			}
			JSONArray tececaiArr = new JSONArray();
			for (int i = 0; i < urlList.size(); i++) {
				Map<String, Object> urlMap = urlList.get(i);
				JSONObject tececaitem = new JSONObject();
				String bindid = CoreUtil.objToStr(urlMap.get("BINDID"));
				String boId = CoreUtil.objToStr(urlMap.get("ID"));
				String tradename = CoreUtil.objToStr(urlMap.get("TRADENAME"));
				String drugname = CoreUtil.objToStr(urlMap.get("DRUGNAME"));
				String drugspec = CoreUtil.objToStr(urlMap.get("DRUGSPEC"));
				String drugunit = CoreUtil.objToStr(urlMap.get("DRUGUNIT"));
				Double drugprice = CoreUtil.objToDouble(urlMap.get("DRUGPRICE"));
				String manufacturer = CoreUtil.objToStr(urlMap.get("MANUFACTURER"));
				

				tececaitem.put("bindid", bindid);
				tececaitem.put("boId", boId);
				tececaitem.put("tradename", tradename);
				tececaitem.put("drugname", drugname);
				tececaitem.put("drugspec", drugspec);
				tececaitem.put("drugunit", drugunit);
				tececaitem.put("drugprice", drugprice);
				tececaitem.put("manufacturer", manufacturer);

				List<FormFile> files = SDK.getBOAPI().getFiles(boId, "PICTURE");
				for(FormFile file : files){
					String downloadUrl = SDK.getBOAPI().getFileDCContext(file).getDownloadURL();
					tececaitem.put("downloadUrl",downloadUrl);
				}

				tececaiArr.add(tececaitem);
			}
				result.put("status", "0");
				result.put("tececaiArr", tececaiArr);

		} catch (Exception e) {
			e.printStackTrace();
			result.put("status", "1");
			result.put("message", e.getMessage());
		}
		return result.toString();
	}
	/**
	 * @Desc 根据网格点地址、药品名称，查询该网格点药品信息
	 * @param uc
	 * @param wgdname  药品地址
	 * @return
	 */
	@Mapping("com.awspaas.user.apps.DrugInformationCheck")
	public static String getDrugInformationCheck(UserContext uc, String wgdname,String drug) {
		JSONObject result = new JSONObject();
		String checksql="";
		String sql="";
		if(!"".equals(drug)) {//商品名
			sql = " and TRADENAME like '%" + drug + "%' ";
		}
		if(!"".equals(drug)) { //药品名
			checksql =" and DRUGNAME like '%" + drug + "%' ";
			
		}
		try {
			String queryUrl =
			    "SELECT BINDID,ID,TRADENAME,DRUGNAME,DRUGSPEC,DRUGUNIT,DRUGPRICE,MANUFACTURER FROM VIEW_SHHT_DRUGLIST " +
					" WHERE GRIDPOINTNAME =  '"+wgdname+"' " + sql;
			List<Map<String, Object>> urlList = DBSql.query(queryUrl, new ColumnMapRowMapper(), new Object[]{});
			String queryUrldrug =
				    "SELECT BINDID,ID,TRADENAME,DRUGNAME,DRUGSPEC,DRUGUNIT,DRUGPRICE,MANUFACTURER FROM VIEW_SHHT_DRUGLIST " +
						" WHERE GRIDPOINTNAME =  '"+wgdname+"' " + checksql;
				List<Map<String, Object>> urlListdrug = DBSql.query(queryUrldrug, new ColumnMapRowMapper(), new Object[]{});
			if((urlList == null || urlList.isEmpty()) && 
					(urlListdrug == null || urlListdrug.isEmpty())) {
				result.put("status", "1");
				result.put("message", "未查询到该服务点提供的药品信息。");
				return result.toString();
			}
			JSONArray tececaiArr = new JSONArray();
			//商品名搜索
			if(urlList.size() >0) {
				for (int i = 0; i < urlList.size(); i++) {
					Map<String, Object> urlMap = urlList.get(i);
					JSONObject tececaitem = new JSONObject();
					String bindid = CoreUtil.objToStr(urlMap.get("BINDID"));
					String boId = CoreUtil.objToStr(urlMap.get("ID"));
					String tradename = CoreUtil.objToStr(urlMap.get("TRADENAME"));
					String drugname = CoreUtil.objToStr(urlMap.get("DRUGNAME"));
					String drugspec = CoreUtil.objToStr(urlMap.get("DRUGSPEC"));
					String drugunit = CoreUtil.objToStr(urlMap.get("DRUGUNIT"));
					Double drugprice = CoreUtil.objToDouble(urlMap.get("DRUGPRICE"));
					String manufacturer = CoreUtil.objToStr(urlMap.get("MANUFACTURER"));
					tececaitem.put("bindid", bindid);
					tececaitem.put("boId", boId);
					tececaitem.put("tradename", tradename);
					tececaitem.put("drugname", drugname);
					tececaitem.put("drugspec", drugspec);
					tececaitem.put("drugunit", drugunit);
					tececaitem.put("drugprice", drugprice);
					tececaitem.put("manufacturer", manufacturer);

					List<FormFile> files = SDK.getBOAPI().getFiles(boId, "PICTURE");
					for(FormFile file : files){
						String downloadUrl = SDK.getBOAPI().getFileDCContext(file).getDownloadURL();
						tececaitem.put("downloadUrl",downloadUrl);
					}

					tececaiArr.add(tececaitem);
				}
			}
			//药品名搜索
			if(urlListdrug.size() >0) {
				for (int j = 0; j < urlListdrug.size(); j++) {
					Map<String, Object> urlMapdrug = urlListdrug.get(j);
					JSONObject tececaitem = new JSONObject();
					String bindid = CoreUtil.objToStr(urlMapdrug.get("BINDID"));
					String boId = CoreUtil.objToStr(urlMapdrug.get("ID"));
					String tradename = CoreUtil.objToStr(urlMapdrug.get("TRADENAME"));
					String drugname = CoreUtil.objToStr(urlMapdrug.get("DRUGNAME"));
					String drugspec = CoreUtil.objToStr(urlMapdrug.get("DRUGSPEC"));
					String drugunit = CoreUtil.objToStr(urlMapdrug.get("DRUGUNIT"));
					Double drugprice = CoreUtil.objToDouble(urlMapdrug.get("DRUGPRICE"));
					String manufacturer = CoreUtil.objToStr(urlMapdrug.get("MANUFACTURER"));
					tececaitem.put("bindid", bindid);
					tececaitem.put("boId", boId);
					tececaitem.put("tradename", tradename);
					tececaitem.put("drugname", drugname);
					tececaitem.put("drugspec", drugspec);
					tececaitem.put("drugunit", drugunit);
					tececaitem.put("drugprice", drugprice);
					tececaitem.put("manufacturer", manufacturer);

					List<FormFile> files = SDK.getBOAPI().getFiles(boId, "PICTURE");
					for(FormFile file : files){
						String downloadUrl = SDK.getBOAPI().getFileDCContext(file).getDownloadURL();
						tececaitem.put("downloadUrl",downloadUrl);
					}

					tececaiArr.add(tececaitem);
				}
			}
				result.put("status", "0");
				result.put("tececaiArr", tececaiArr);

		} catch (Exception e) {
			e.printStackTrace();
			result.put("status", "1");
			result.put("message", e.getMessage());
		}
		return result.toString();
	}
}
